package com.example.uit_management;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TextView;

public class ArrayAdapterDialogRegistry_vs2 extends BaseAdapter implements OnCheckedChangeListener {
	// XML node keys	
	HashMap<String, String> array = new HashMap<String, String>(); 
	/**Biên kiểu Activity*/
    private Activity activity;  
    /**Biên kiểu ArrayList<HashMap<String, String>> lưu số mon có thể đăng kí năm nay */
    private ArrayList<HashMap<String, String>> data;  

    
    private static LayoutInflater inflater=null;   
    /**Hàm khởi tạo ArrayAdapterRegistry */
    public ArrayAdapterDialogRegistry_vs2(Activity a, ArrayList<HashMap<String, String>> d) {
    	activity = a;
        data=d;              
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    /** Lấy giá trị Mã và tên môn học (subjectName)*/
    public String getItemAtIndex(int index){
        return data.get(index).get(JsonParser_course.KEY_subjectName);
    } 
    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi=convertView;      
        if(convertView==null)
            vi = inflater.inflate(R.layout.registry_dialog_row_vs2, null);
        // Get ID of Xml layout                
        array = data.get(position);  
        // Lấy ID các text view và các check box
    	Log.e(ArrayAdapterDialogRegistry.class.toString(),"-------------Lay ID");

        TextView subjectName = (TextView)vi.findViewById(R.id.subjectName_RadioButton_vs2); 
        TextView courseSize = (TextView)vi.findViewById(R.id.courseSize_RadioButton_vs2); 
        TextView lecturerName = (TextView)vi.findViewById(R.id.lecturerName_RadioButton_vs2); 
        TextView facultyID = (TextView)vi.findViewById(R.id.facultyID_RadioButton_vs2); 
        TextView schedule = (TextView)vi.findViewById(R.id.schedule_RadioButton_vs2); 
        TextView subjectTheory = (TextView)vi.findViewById(R.id.subjectTheory_RadioButton_vs2); 
        RadioButton radiobutton=(RadioButton)vi.findViewById(R.id.check_RadioButton_vs2); 
        
        // thiết lặp bộ lắng nghe cho check box
        radiobutton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
            	RadioButton rb = (RadioButton) v.findViewById(R.id.check_RadioButton_vs2);
                if (rb.isChecked()) {
                	//Log.e(ArrayAdapterRegistry.class.toString(),"Check"+position); 
                	//Check_array[position]="1"; 
                	for (int i = 0; i < Activity_list_registry_vs2.array_int_check.length; i++) {
                		Log.e(ArrayAdapterDialogRegistry_vs2.class.toString(),"Check tai int  :"+i+"= "+Activity_list_registry_vs2.array_int_check[i]); 
                		if(Activity_list_registry_vs2.array_int_check[i]<0){
                    		break;
                    	}
                		ArrayAdapterRegistry_vs2.Check_array[Activity_list_registry_vs2.array_int_check[i]]=""+0;                 	
					}
                	ArrayAdapterRegistry_vs2.Check_array[Activity_list_registry_vs2.array_int_check[position]]=""+1;
                	for (int j = 0; j < ArrayAdapterRegistry_vs2.Check_array.length; j++) {
                		Log.e(ArrayAdapterDialogRegistry_vs2.class.toString(),"Check tai :"+j+"= "+ArrayAdapterRegistry_vs2.Check_array[j]); 
					}
                	Activity_list_registry_vs2.dialog.dismiss();
                	//Activity_list_registry.listV.invalidateViews();
                	Activity_list_registry_vs2.adapter.notifyDataSetChanged();

                	// do some operations here
                } 
//                else if (!rb.isChecked()) {
//                	//Log.e(ArrayAdapterRegistry.class.toString(),"Check"+position); 
//                	//Check_array[position]="0";
//                    // do some operations here
//                }               
            }
        });   
        //
        // Tiến hành thiết lập các text view và ID đã lấy
       
        
    	Log.e(ArrayAdapterDialogRegistry_vs2.class.toString(),"-------------set Text");

        subjectName.setText(array.get(JsonParser_course.KEY_courseID)+"_"+array.get(JsonParser_course.KEY_subjectName));
        courseSize.setText(array.get(JsonParser_course.KEY_courseSize)+"/"+array.get(JsonParser_course.KEY_courseMaxSize));
        lecturerName.setText(array.get(JsonParser_course.KEY_lecturerName));
        facultyID.setText("Khoa :"+array.get(JsonParser_course.KEY_facultyID));
        schedule.setText(array.get(JsonParser_course.KEY_schedule));
        subjectTheory.setText("TCLT :"+array.get(JsonParser_course.KEY_subjectTheory)+"TH :"+array.get(JsonParser_course.KEY_subjectPractice));    
    	Log.e(ArrayAdapterDialogRegistry.class.toString(),"-------------Finish"+position);
    	return vi;
    }

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		
	}   	
}